package com.smt.modules.app.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.common.enums.Code;
import com.smt.common.utils.EnumToKeyUtil;
import com.smt.common.utils.PageUtils;
import com.smt.common.utils.R;
import com.smt.modules.app.annotation.Login;
import com.smt.modules.bus.entity.EventManagementEntity;
import com.smt.modules.bus.service.EventService;
import com.smt.modules.bus.vo.EventManagementVo;
import com.smt.modules.bus.vo.ProjectTaskVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.java.Log;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 *  事件管理
 *
 * @author zhangyi
 * @date 2018/10/10 17:12
 */
@RestController
@RequestMapping("/app/event")
@Api(tags = {"AppEventManageController"},description = "事件管理(客户端接口)")
public class AppEventManageController {
    @Resource
    private EventService eventService;

    /**
     * 添加事件
     * @param eventManagementVo
     * @return
     */
    @Login
    @PostMapping("/add")
    @ApiOperation(value = "/add",notes = "添加事件",httpMethod = "POST")
    public R add(@RequestBody @Validated EventManagementVo eventManagementVo,@RequestAttribute("userId")Long userId){
        R add = eventService.add(eventManagementVo,userId);
        return add;
    }

    /**
     * 获取事件列表
     * @return
     */
    @Login
    @GetMapping("/list/{projectId}")
    @ApiOperation(value = "获取当前用户的事件列表",httpMethod = "GET")
    public R list(@PathVariable("projectId") Long projectId,
                  @RequestAttribute("userId") Long userId){
        List<EventManagementEntity> events = eventService.selectList(new EntityWrapper<EventManagementEntity>()
                .eq("member_id", userId)
                .eq("project_id",projectId)
                .orderBy("create_time",true));
        return R.ok().put("events",events);
    }

    /**
     * 删除事件列表
     * @return
     */
    @DeleteMapping("/delete/{eventId}")
    @ApiOperation(value = "删除事件列表",httpMethod = "DELETE")
    public R delete(@PathVariable("eventId") Long eventId){
        boolean b = eventService.deleteById(eventId);
        return b?R.ok("删除成功"):R.error(EnumToKeyUtil.toInt(Code.DELETE_DATA_ERROR),"删除失败");
    }
    /**
     * 编辑事件模板
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "编辑事件模板",httpMethod = "PUT")
    public R edit(@RequestBody EventManagementVo eventManagementVo){
        EventManagementEntity eventManagementEntity = eventManagementVo.copyPropertiesTo(new EventManagementEntity());
        boolean b = eventService.updateById(eventManagementEntity);
        return b?R.ok("编辑事件模板成功"):R.error(EnumToKeyUtil.toInt(Code.UPDATE_FAIL),"编辑事件模板失败");
    }
}
